package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Notice;
import cn.wolfcode.crm.query.NoticeQuery;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.INoticeService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") NoticeQuery qo){
        PageInfo<Notice> pageInfo = noticeService.query(qo);
        model.addAttribute("result", pageInfo);
        return "notice/list";
    }

    @RequestMapping("/show")
    public String show(Long id, Model model){
        model.addAttribute("notice", noticeService.get(id));
        return "notice/show";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            noticeService.delete(id);
        }
        return new JsonResult(true,"删除成功");
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Notice notice){
        if (notice.getId() != null) {
            noticeService.update(notice);

        }else {
            noticeService.save(notice);
        }
        return new JsonResult(true,"操作成功");
    }
}
